//
//  AESCryptObject.m
//  HSEDU
//
//  Created by oceans on 2018/7/12.
//  Copyright © 2018年 xvv. All rights reserved.
//

#import "AESCryptObject.h"
#import "AESCipher.h"

static NSString *key = @"3c6e0b8a9c15224a8228b9a98ca1531d";

@implementation AESCryptObject

+ (NSString *)aes_crypt:(NSString *)data {
    
    int pos = 0;
    NSString *iv = [self getIV:&pos];
    NSString *prefix = [NSString stringWithFormat:@"%c", pos+48];
    NSString *enString = aesEncryptString(data, key, iv);
    NSData *enData = [[prefix stringByAppendingString:enString] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64 = [enData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    return base64;
}

+ (NSString *)getIV:(int *)pos {
    
    int rand = arc4random() % 33;
    *pos = rand;
    NSString *iv = [[key stringByAppendingString:key] substringWithRange:NSMakeRange(rand, 16)];
    return iv;
}

@end
